package main;
import java.util.Vector;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
/**
 * Animate a sprite on canvas using a simple algorithm
 * to recover from collisions.
 *
 * @author Karel Herink
 */
public class SpriteRandomMovement implements Runnable {

    protected int _speed = 5;

    /**
     * @return the _speed
     */
    public int getSpeed() {
        return _speed;
    }

    /**
     * @param aSpeed the _speed to set
     */
    public void setSpeed(int aSpeed) {
        _speed = aSpeed;
    }
    protected  Sprite sprite;
    protected int previousDirection = GameCanvas.DOWN;
    protected int direction = GameCanvas.DOWN;
    protected boolean interrupted;
    protected int[] downSeq;
    protected int[] upSeq;
    protected int[] leftSeq;
    protected int[] rightSeq;
    protected Sprite[] DoiPhuongGayHai;
    protected int nDoiPhuongGayHai = 0;
    protected TiledLayer[] VatCanTinh;
    protected int nVatCanTinh = 0;
    protected Sprite[] VatCanDong;
    protected int nVatCanDong = 0;

    
    public SpriteRandomMovement(Sprite sprite) {
        this.sprite = sprite;
        DoiPhuongGayHai = new Sprite[5];
    }

    public void ThemDoiTuongGayHai(Sprite[] s, int n) {
        this.DoiPhuongGayHai = s;
        this.nDoiPhuongGayHai = n;
    }

    public void ThemVatCanTinh(TiledLayer[] layers, int n) {
        this.VatCanTinh = layers;
        this.nVatCanTinh = n;
    }

    public void ThemVatCanDong(Sprite[] s, int n) {
        this.VatCanDong = s;
        this.nVatCanDong = n;
    }

    public boolean VaChamVoiDoiTuongGayHai() {
        for (int i = 0; i < this.nDoiPhuongGayHai; i++) {
            if (this.sprite.collidesWith(DoiPhuongGayHai[i], true)) {
                return true;
            }
        }
        return false;
    }

    public boolean VaChamVatCanDong() {
        for (int i = 0; i < this.nVatCanDong; i++) {
            if (this.sprite.collidesWith(VatCanDong[i], true)) {
                return true;
            }
        }
        return false;
    }

    public boolean VaChamVoiVatCanTinh() {
        for (int i = 0; i < this.nVatCanTinh; i++) {
            if (this.sprite.collidesWith(this.VatCanTinh[i], false)) {
                return true;
            }
        }
        return false;
    }
    public void setSequences(int[] downSeq, int[] upSeq, int[] leftSeq, int[] rightSeq) {
        this.downSeq = downSeq;
        this.upSeq = upSeq;
        this.leftSeq = leftSeq;
        this.rightSeq = rightSeq;
    }

    public void stop() {
        this.interrupted = true;
    }

    public void run() {
        while (!this.interrupted) {
            if (this.direction == GameCanvas.DOWN) {
                if (this.previousDirection != this.direction) {
                    this.sprite.setFrameSequence(this.downSeq);
                    this.previousDirection = this.direction;
                }
                this.sprite.move(0, getSpeed());
                if (VaChamVoiVatCanTinh()|| VaChamVatCanDong()) {
                    this.sprite.move(0, -getSpeed());
                    this.direction = GameCanvas.LEFT;
                    continue;
                }
            } else if (this.direction == GameCanvas.UP) {
                if (this.previousDirection != this.direction) {
                    this.sprite.setFrameSequence(this.upSeq);
                    this.previousDirection = this.direction;
                }
                this.sprite.move(0, -getSpeed());
                if (VaChamVoiVatCanTinh()|| VaChamVatCanDong()) {
                    this.sprite.move(0, getSpeed());
                    this.direction = GameCanvas.RIGHT;
                    continue;
                }
            } else if (this.direction == GameCanvas.LEFT) {
                if (this.previousDirection != this.direction) {
                    this.sprite.setFrameSequence(this.leftSeq);
                    this.previousDirection = this.direction;
                }
                this.sprite.move(-getSpeed(), 0);
                if (VaChamVoiVatCanTinh() || VaChamVatCanDong()) {
                    this.sprite.move(getSpeed(), 0);
                    this.direction = GameCanvas.UP;
                    continue;
                }
            } else if (this.direction == GameCanvas.RIGHT) {
                if (this.previousDirection != this.direction) {
                    this.sprite.setFrameSequence(this.rightSeq);
                    this.previousDirection = this.direction;
                }
                this.sprite.move(getSpeed(), 0);
                if (VaChamVoiVatCanTinh()|| VaChamVatCanDong()) {
                    this.sprite.move(-getSpeed(), 0);
                    this.direction = GameCanvas.DOWN;
                    continue;
                }
            }
            try {
                Thread.sleep(300);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
}
